//
//  LYOrderListView.swift
//  LYOnlineBanking
//
//  Created by 李永彬 on 2025/7/26.
//

import UIKit

class LYOrderListView: UIView {
    
    var dataSources: [OrderListSeptenier] = [] {
        didSet {
            self.tableView.reloadData()
        }
    }
    
    var didClickCellClourse:((OrderListSeptenier) -> Void)?
    
    lazy var tableView: UITableView = {
        let tableView = UITableView(frame: .zero, style: .grouped)
        tableView.register(LYOrderListCell.self, forCellReuseIdentifier: "LYOrderListCell")
        tableView.dataSource = self
        tableView.delegate = self
        tableView.separatorStyle = .none
        tableView.rowHeight = UITableView.automaticDimension
        tableView.estimatedRowHeight = 120
        tableView.backgroundColor = .clear
        tableView.tableHeaderView = UIView(frame: CGRect(x: 0, y: 0, width: 0, height: CGFloat.leastNonzeroMagnitude))
        tableView.sectionHeaderHeight = CGFloat.leastNonzeroMagnitude
        tableView.contentInsetAdjustmentBehavior = .never
        return tableView
    }()
 
    override init(frame: CGRect) {
        super.init(frame: frame)
        commonInitSubviews()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func commonInitSubviews() {
        addSubview(tableView)
        tableView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
    }
}

extension LYOrderListView: UITableViewDataSource, UITableViewDelegate {
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return dataSources.count
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "LYOrderListCell", for: indexPath) as! LYOrderListCell
        cell.configure(with: dataSources[indexPath.section])
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        let septenier = dataSources[indexPath.section]
        didClickCellClourse?(septenier)
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        
        let aspectRatio = 103.0 / 346.0
        let imageHeight = (SCREEN_WIDTH - LYOrderListCell.leftRMargin * 2) * aspectRatio
        return imageHeight + LYOrderListCell.top
    }
    
    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        
        let septenier = dataSources[section]
        
        let view = UIView()
        
        if let area = septenier.maciair?.area,
           !area.isEmpty {
            
            let imageView = UIImageView()
            imageView.image = UIImage(named: "order_warning")
            imageView.frame = CGRect(x: 20, y: 7, width: 17, height: 17)
            view.addSubview(imageView)
            
            let label = StrokeLabel()
            label.textAlignment = .left
            label.font = UIFont.systemFont(ofSize: 12)
            label.frame = CGRect(x: 45, y: 10, width: SCREEN_WIDTH - 60, height: 17)
            label.textColor = UIColor(hex: "#FBD004")
            view.addSubview(label)
            
            label.text = area
        }
        return view
    }
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        let septenier = dataSources[section]
        
        if let area = septenier.maciair?.area,
           !area.isEmpty {
            return 30
        }

        return 0
    }
}

class StrokeLabel: UILabel {
    var outlineWidth: CGFloat = 2.0 {
        didSet { setNeedsDisplay() }
    }
    
    var outlineColor: UIColor = UIColor(hex: "#000000") {
        didSet { setNeedsDisplay() }
    }
    
    override func drawText(in rect: CGRect) {
        let strokeTextAttributes: [NSAttributedString.Key: Any] = [
            .strokeColor: outlineColor,
            .strokeWidth: -outlineWidth, // 负值表示同时描边和填充
            .foregroundColor: textColor ?? UIColor(hex: "#FBD004"),
            .font: font ?? UIFont.systemFont(ofSize: 12)
        ]
        
        let attributedString = NSAttributedString(
            string: text ?? "",
            attributes: strokeTextAttributes
        )
        
        attributedString.draw(in: rect)
    }
}



